package com.zqq.goods.presenter

import com.zqq.baselibrary.ext.excute
import com.zqq.baselibrary.http.rx.BaseSubscriber
import com.zqq.baselibrary.presenter.BasePresenter
import com.zqq.goods.presenter.view.CartListView
import com.zqq.goods.service.CartService
import com.zqq.goods.service.bean.CartGoods
import javax.inject.Inject

/**
 * 购物车 Presenter
 * wc
 * @author zqq on 2018/8/5.
 */
class CartListPresenter @Inject constructor() : BasePresenter<CartListView>() {

    @Inject
    lateinit var cartService: CartService

    /**
     * 1. 获取购物车列表
     */
    fun getCartList() {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        cartService.getCartList()
                .excute(object : BaseSubscriber<MutableList<CartGoods>?>(mView) {
                    override fun onNext(t: MutableList<CartGoods>?) {
                        mView.onGetCartListResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        val cartList = getCartGoodsList()
                        mView.onGetCartListResult(cartList)
                    }
                }, lifecycleProvider)

    }


    /**
     * 2.删除购物车商品
     */
    fun deleteCartList(list: List<Int>) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()
        cartService.deleteCartList(list)
                .excute(object : BaseSubscriber<Boolean>(mView) {
                    override fun onNext(t: Boolean) {
                        super.onNext(t)
                        mView.onDeleteCartResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        mView.onDeleteCartResult(true)
                    }
                }, lifecycleProvider)
    }


    /**
     * 3.提交购物车商品(不是真正的订单，还有一个订单确认页)
     * 预生成订单
     */
    fun submitCart(list: MutableList<CartGoods>, totalPrice: Long) {
        if (!checkNetWork()) {
            return
        }
        mView.showLoading()

        cartService.submitCart(list, totalPrice)
                .excute(object : BaseSubscriber<Int>(mView) {
                    override fun onNext(t: Int) {
                        super.onNext(t)
                        mView.onSubmitCartListResult(t)
                    }

                    override fun onError(e: Throwable) {
                        super.onError(e)
                        mView.onSubmitCartListResult(12345)
                    }
                }, lifecycleProvider)

    }


    private fun getCartGoodsList(): MutableList<CartGoods>? {
        var lists = mutableListOf<CartGoods>()

        var icon1 = "https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=3bc7aa97e21190ef01fb95d9f620fa2b/4bed2e738bd4b31c39f3eec182d6277f9f2ff8f9.jpg"
        var icon2 = "http://pic32.nipic.com/20130902/13395269_103525238102_2.jpg"

        var cart1 = CartGoods(1, 1314, "GoodsDesc", icon1, 25, 50, "红色,XXL", true)
        var cart2 = CartGoods(2, 1314, "利达无情", icon2, 999, 20, "规格,1盒", false)
        var cart3 = CartGoods(3, 1214, "GoodsDesc", icon1, 199, 50, "黑白,5颦", true)
        var cart4 = CartGoods(4, 1514, "GoodsDesc", icon2, 998, 30, "六股,8纵", false)

        lists.add(cart1)
        lists.add(cart2)
        lists.add(cart3)
        for (i in 1..10) {
            lists.add(cart4)
        }

        return lists
    }


}